﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ecopsy.Tools;
using AgencyEntityLib.BaseEntity;
using FormControllers;
using Ecopsy.DataTier;
using SqlLibrary;

public partial class iCRM_Tasks : iPageController 
{
    private const string AllEmpl = "не учитывать";

    
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {         
            txtStart .Text = DateTimeTools.GetFisrtDayInMonth (DateTime .Now ).ToShortDateString ();  
            txtFinish.Text = DateTimeTools.GetLastDayInMonth (DateTime .Now ).ToShortDateString ();
            //ctlTaskList1.DataBind();
            ctlFilterBar1.DataBind();
            ctlEmpCombo1.DataBind();
            ctlFilterBar1_OnFindClik(ctlFilterBar1.Lexeme);
        }

        OnInit();
    }

    protected void OnInit()
    {
        ctlEmpCombo1.OnSelectedIndexChanged = EmplSelectedIndexChanged;

        ctlFilterSave1.SaveOnClick = ctlFilterSave1_SaveOnClick;
        ctlFilterSave1.OkOnClick = ctlFilterSave1_OkOnClick;        
        ctlFilterSave1.FilterType = Filter.FilterType.TaskList;
        ctlFilterSave1.AddKey = CategoriaID;
        
        

        ctlFilterBar1.OnFindClik = ctlFilterBar1_OnFindClik;
        ctlFilterBar1.OnFilterListClik = ctlFilterBar1_OnFilterListClik;
        ctlFilterBar1.OnFilterSaveClik = ctlFilterBar1_OnFilterSaveClik;
        ctlFilterBar1.HintServiceMethod = "GetTaskListParams";
        
        
        ctlFilterList1.OnApp_Click = ctlFilterList1_OnApp_Click;
        ctlFilterList1.FilterType = Filter.FilterType.TaskList;
        ctlFilterList1.AddKey = CategoriaID;

        ModalPopupext_FilterSave.DataBind();
        ModalPopupext_FilterList.DataBind(); 
    }

    private bool ctlFilterList1_OnApp_Click(ListWhere _Where)
    {
        ctlTaskList1.Where  = _Where;
        ctlFilterBar1.Lexeme = _Where.ListLoadStatment.Lexeme;
        
        ctlFilterSave1.DataBind(); 
        ctlFilterBar1.DataBind(); 
        ctlTaskList1.DataBind();
        return true;
    }

    private bool ctlFilterBar1_OnFilterSaveClik()
    {
        ModalPopupext_FilterSave.Show();
        return true;
    }

    private bool ctlFilterBar1_OnFilterListClik()
    {          
        ctlFilterList1.MyDataBind();
        ModalPopupext_FilterList.Show();        
        return true;
    }

    private ListWhere ctlFilterSave1_SaveOnClick()
    {      
        return (ListWhere)ctlTaskList1.Where.Clone();
    }
 
    private bool ctlFilterSave1_OkOnClick()
    {
        ModalPopupext_FilterSave.DataBind();
        ModalPopupext_FilterSave.Hide();
        return true;
    }

    private bool ctlFilterBar1_OnFindClik(string sFindStr)
    {
        if (ctlEmpCombo1.CurrentValue.IndexOf(AllEmpl) == -1)
            ((TaskList.sqlLoadStatment)ctlTaskList1.Where.ListLoadStatment).EmplID = ctlEmpCombo1.CurrentValue.ToString();  
        else
            ((TaskList.sqlLoadStatment)ctlTaskList1.Where.ListLoadStatment).EmplID = string.Empty;
            

         
        ctlTaskList1.Lexeme = ctlFilterBar1.Lexeme;
        ctlTaskList1.DataBind();
        return true;
    }


    #region SavePanel

    protected string GetTargetControlID_FilterSave()
    {
        return ctlFilterBar1.BtnAddFilter_ClientID;
    }

    public string GetOkControlID_FilterSave()
    {

        return ctlFilterSave1.btnSave_ClientID;
    }

    public string GetCancelControlID_FilterSave()
    {

        return ctlFilterSave1.BtnCancel_ClientID;
    }

    #endregion

    #region ListPanel

    protected string GetTargetControlID_FilterList()
    {
        return ctlFilterBar1.BtnFilterList_ClientID;
    }

    public string GetOkControlID_FilterList()
    {

        return ctlFilterList1.btnSave_ClientID;
    }

    public string GetCancelControlID_FilterList()
    {

        return ctlFilterList1.BtnCancel_ClientID;
    }

    #endregion


    protected DateTime dtStart { get { return  Convert.ToDateTime(txtStart.Text); } }
    protected DateTime dtFinish { get { return Convert.ToDateTime(txtFinish.Text); } }


   

    protected void btnFind_Click(object sender, ImageClickEventArgs e)
    {
        
        //string sFlt = txtSearch.Text;
        //int errPosition = -1;
        //if (!SqlLibrary.ExpressionChecker.CheckStatment(ref sFlt, ref errPosition))
        //{
        //    ScriptManager.RegisterStartupScript(this, GetType(), "AlertBadFiltr", "alert('Ошибочное выражение фильтрации');", true);
        //    return;
        //}

        //ctlTaskList1.Lexeme = sFlt;
        //ctlTaskList1.DataBind();
    }

    protected string CategoriaID { get { return Request.QueryString["CtegoriaID"]; } }




    protected void btnDay_Click(object sender, EventArgs e)
    {
        txtStart.Text = DateTime.Now.ToShortDateString();
        txtFinish.Text = DateTime.Now.ToShortDateString();
        ctlFilterBar1_OnFindClik(ctlFilterBar1.Lexeme);

    }
    protected void btnWeek_Click(object sender, EventArgs e)
    {
        txtStart.Text = DateTimeTools.GetFisrtDayInWeek(DateTime.Now).ToShortDateString();        
        txtFinish.Text = DateTimeTools.GetLastDayInWeek(DateTime.Now).ToShortDateString();
        ctlFilterBar1_OnFindClik(ctlFilterBar1.Lexeme);

    }
    protected void btnMonth_Click(object sender, EventArgs e)
    {
        txtStart.Text = DateTimeTools.GetFisrtDayInMonth (DateTime.Now).ToShortDateString();
        txtFinish.Text = DateTimeTools.GetLastDayInMonth (DateTime.Now).ToShortDateString();
        ctlFilterBar1_OnFindClik(ctlFilterBar1.Lexeme);

    }

    protected bool EmplSelectedIndexChanged(ListItem item)
    {
        ctlFilterBar1_OnFindClik(ctlFilterBar1.Lexeme);
        return true;
    }

  
}
